var forword = encodeURIComponent(window.location.href);
var backUrl = $.getUrlVar("back");
var uid = $.getUrlVar("uid");

var vm=new Vue({
  el:"#app",
  data:{
    title:'',
    tabs:0,
    addShow: false,
    infos:[],
    topics:[],
    asks:[],
    circle:[],
    user:{},
    reqOut: false, //判断请求数据是否已完
    isLoading:true,
    pageSize:PAGESIZE,
    pageIndex:1,
    isTrue:false
  },
  created:function(){
    var _self = this;
    // 顶部个人信息
    var data = {
      uid:uid
    }
    Common.ajaxCall({ url: 'user_getUserDetailInfo', datatype: 'json', type: 'post', data: data}, function (data) {
      if(data.error_code==0){
          _self.user=data.data;
        
      }else{
          alert(data.msg);
      }
    });
    $(window).scroll(function() {
        var topH = $(window).scrollTop();
        var userH = $('.vault').height();
        if (topH > userH) {
            $('.tabFixed').addClass('top');
            $('.tabBox').css('padding-top', $('.tabFixed').outerHeight());
        } else {
            $('.tabFixed').removeClass('top');
            $('.tabBox').css('padding-top', '0');
        }
    });
  },
  compiled: function(){
    var _self = this;
    var top = $(document).scrollTop();
    $(document).scroll(function () {
      if(top!=$(document).scrollTop()){
        // 上下滚动
        var docHeight = $(document).height(),
            winHeight = $(window).height();
        if ((docHeight > winHeight)&&($(document).scrollTop() >= docHeight - winHeight)) {
            if (!_self.reqOut) {
                _self.reqOut = true;
                var str = '<img class="loading-img" src="images/loading.gif"></img>';
                myLoad.pullLoad('quesCont', str);
                _self.willShow === 'modify' ? '' : _self.loadlist(_self.tabs);

            }
        }
      }else{
          // 左右滚动
      }
    });

    _self.loadlist(_self.tabs);
  },
  methods:{
    loadlist:function(tabs){
      var _self = this;
      var vModal,target;  //用来储存不同的tab页面对应的对象
      switch(tabs){       //根据tab的切换来设置sortType的值
        case 0:
          // 文档
          var url = 'user_getMyArticle';
          var data = {
            uid:uid,
            pageSize:_self.pageSize,
            pageIndex:_self.pageIndex
          } 
          
          vModal = _self;
          target = _self.infos;
        break;
        case 1:// 话题
          var url = 'user_getMyTopic';
          var data = {
            uid:uid,
            pageSize:_self.pageSize,
            pageIndex:_self.pageIndex
          }; 

          vModal = _self;
          target = _self.topics;
        break;
        case 2:// 提问
          var url = 'user_getMyAsk';
          var data = {
            uid:uid,
            pageSize:_self.pageSize,
            pageIndex:_self.pageIndex
          }; 

          vModal = _self;
          target = _self.asks;
        break;
        case 3:
          var url = 'user_getMyAnswer';
          var data = {
            uid:uid,
            pageSize:_self.pageSize,
            pageIndex:_self.pageIndex
          }; 

          vModal = _self;
          target = _self.asks;
        break;
        case 4:
          var url = 'user_getMyCircle';
          var data = {
            uid:uid,
            pageSize:_self.pageSize,
            pageIndex:_self.pageIndex
          }; 

          vModal = _self;
          target = _self.circle;
        break;
        default:
           
        break; 
      }

      if(!vModal.isLoading) return;
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: url, datatype: 'json', type: 'post', data: data}, function (data) {
          if(data.error_code==0 && data.data.length > 0){
              if(vModal.pageIndex==1){
                target.length = 0;
              }
              for(var i = 0; i < data.data.length; i++){
                target.push(data.data[i]);
              }
              _self.reqOut = false;

              //实际数据与请求的数据量比较
              if (data.data.length >= vModal.pageSize) {
                  myLoad.pullHide(); //加载后，把效果去掉
              } else {
                  _self.reqOut = true;
                  myLoad.noDate(); //暂无数据
                  vModal.isLoading = false;
              }
              
          }else{
              _self.reqOut = true;
              myLoad.noDate(); //暂无数据
              vModal.isLoading = false;
          }

          vModal.pageIndex++;
      });
      
    },
    back: function() {
        window.location.href = decodeURIComponent(backUrl);
    },
    add:function(){
      if(this.addShow == false){
        this.addShow = true;
      }else{
        this.addShow = false;
      }
    },
    fn:function(flag,uid,isAbleClick){
      var _self = this;
      _self.title = _self.user.userName;

      if(!isAbleClick) return;
      switch(flag){
        case 'dynamic':
          window.location.href = "dynamicKnowledge.html?uid="+uid+"&back="+forword;

        break;
        case 'collect':
          //this.willShow = flag;
          window.location.href = "myCollect.html?uid="+uid+"&back="+forword;
        break;
        case 'community':
          this.willShow = flag;
          _self.tabs = 5;   //查看加入圈子
          _self.circle = [];
          _self.isLoading = true;

          _self.loadlist(_self.tabs);
        break;
        case 'modify':
          
          _self.willShow = flag;
          $('.userModify li').bind('click',function(){
            //console.log($(this).index());
            var e = $(this).find('input');
            if(e.data('modifiable')){
              _self.willShow = 'modifyText';
              $("#huserName").hide();

              _self.name = e.attr('name');
              _self.value = e.val();
              _self.typeID = e.attr('name'); //将要修改的字段保存下来
            }
          });
        break;
      }
    },
    slides:function(index,e){
        var _self = this;
        _self.tabs = index;
        
        $(e.currentTarget).addClass('on').siblings().removeClass('on');
        
        var data = {
          isLoading:true,
          pageSize:PAGESIZE,
          pageIndex:1,
          infos:[],
          asks:[],
          topics:[]
        };
        _self = $.extend(_self,data);

        _self.loadlist(_self.tabs);
    },
    toCircleList:function(id){
      window.location.href = "circleList.html?id=" + id + "&back=" + forword;
    },
    toArticleDetail:function(id){
      window.location.href = "articleDetail.html?id=" + id + "&back=" + forword;
    },
    toCircleDetail:function(id){
      window.location.href = "circleDetail.html?id=" + id + "&back=" + forword;
    },
    toAskDetail:function(id){
      window.location.href = "askDetail.html?id=" + id + "&back=" + forword;
    },
    loadPhoto:function(){
      // 轮播比例
      myJS.imgScale('.j_1', '1', '1');
      myJS.ImgFull('.loadImg','images/liby.png');
    }
  },
  watch:{
    "circle": function(){
      this.loadPhoto();
    }
  }
});